package com.igeekhome.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.igeekhome.mapper.ScanCarMapper;
import com.igeekhome.pojo.ScanCar;
import com.igeekhome.pojo.ScanCarExample;
import com.igeekhome.service.ICarService;
import com.igeekhome.util.ResultBean;
import com.igeekhome.util.TableResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author Romeo
 */
@Service("carService")
public class CarServiceImpl implements ICarService {

    @Autowired
    private ScanCarMapper scanCarMapper;

    @Override
    public TableResultBean list(Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        ScanCarExample example = new ScanCarExample();
        example.createCriteria().andStatusEqualTo(1);
        List<ScanCar> scanCars = scanCarMapper.selectByExample(example);
        PageInfo<ScanCar> pageInfo = new PageInfo<>(scanCars);
        if(scanCars != null){
            return TableResultBean.ok(pageInfo.getTotal(),scanCars);
        }
        return TableResultBean.fail("暂无数据");
    }

    @Override
    public ResultBean add(ScanCar scanCar, Integer networkId, BigDecimal MaxEight, BigDecimal MaxAuthorized , BigDecimal MaxHundredFuel) {
        scanCar.setNetworkId(networkId);
        scanCar.setStatus(1);


        ResultBean resultBean = toCheck(scanCar, MaxEight, MaxAuthorized, MaxHundredFuel);
        if(resultBean != null){
            return resultBean;
        }

        int insert = scanCarMapper.insert(scanCar);
        if(insert > 0){
            return ResultBean.success();
        }
        return ResultBean.fail("新增失败");
    }

    @Override
    public ResultBean edit(ScanCar scanCar, BigDecimal MaxEight, BigDecimal MaxAuthorized , BigDecimal MaxHundredFuel) {
        ResultBean resultBean = toCheck(scanCar, MaxEight, MaxAuthorized, MaxHundredFuel);
        if(resultBean != null){
            return resultBean;
        }
        int update = scanCarMapper.updateByPrimaryKeySelective(scanCar);
        if(update > 0) {
            return ResultBean.success();
        }
        return ResultBean.fail("更新失败");
    }

    @Override
    public ResultBean loginDelete(String idsStr) {

        if(idsStr.contains(",")){
            idsStr = idsStr.substring(0, idsStr.length() - 1);
        }
        if(!idsStr.contains(",")){
            // 单个删除
            ScanCar scanCar = new ScanCar();
            scanCar.setStatus(0);

            ScanCarExample example = new ScanCarExample();
            example.createCriteria().andCarIdEqualTo(Integer.parseInt(idsStr));
            int delete = scanCarMapper.updateByExampleSelective(scanCar, example);
            if(delete > 0) {
                return ResultBean.success();
            }
        } else {
            // 批量删除
            int delete = scanCarMapper.deleteByidsStr(idsStr);
            if(delete > 0) {
                return ResultBean.success();
            }
        }


        return ResultBean.fail("删除失败");
    }


    private ResultBean toCheck(ScanCar scanCar, BigDecimal MaxEight, BigDecimal MaxAuthorized , BigDecimal MaxHundredFuel){
        BigDecimal netWeight = scanCar.getNetWeight();
        BigDecimal authorizedLoad = scanCar.getAuthorizedLoad();
        BigDecimal hundredFuel = scanCar.getHundredFuel();
        if(netWeight == null || authorizedLoad == null || hundredFuel == null){
            return ResultBean.fail("重要参数信息不可为空");
        }
        if(MaxEight.compareTo(netWeight) == -1 || MaxAuthorized.compareTo(authorizedLoad) == -1 || MaxHundredFuel.compareTo(hundredFuel) == -1){
            return ResultBean.fail("参数信息不合法");
        }
        return null;
    }
}
